Conditions | 1 |
Paths | 2 |
Total Lines | 265 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 4 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | var chai = require('chai'); |
||
18 | describe('cmsTemplates.prepare', function() { |
||
19 | before( function(done) { |
||
20 | Manager.instance.init() |
||
21 | .then(function () { |
||
22 | this.fixture = { |
||
23 | visibleTrue: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'prepare-tag-visible-true.html'), 'utf-8'), |
||
24 | visibleFalse: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'prepare-tag-visible-false.html'), 'utf-8'), |
||
25 | text: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'prepare-tag-abe-text.html'), 'utf-8'), |
||
26 | attribute: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'prepare-tag-abe-attribute.html'), 'utf-8'), |
||
27 | attributeConcat: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'prepare-tag-abe-attribute-concat.html'), 'utf-8'), |
||
28 | attributeMultiple: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'prepare-tag-abe-attribute-multiple.html'), 'utf-8'), |
||
29 | source: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'prepare-tag-abe-source.html'), 'utf-8'), |
||
30 | each: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'prepare-tag-abe-each.html'), 'utf-8'), |
||
31 | eachMultiple: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'prepare-tag-abe-each-multiple.html'), 'utf-8'), |
||
32 | eachMultiplePrepared: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'prepare-tag-abe-each-multiple-prepared.html'), 'utf-8'), |
||
33 | eachVariable: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'prepare-tag-abe-each-variable.html'), 'utf-8'), |
||
34 | rawHandlebar: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'prepare-raw-handlebars.html'), 'utf-8'), |
||
35 | noHtml: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'prepare-tag-nohtml.html'), 'utf-8') |
||
36 | } |
||
37 | done() |
||
38 | |||
39 | }.bind(this)) |
||
40 | }); |
||
41 | |||
42 | /** |
||
43 | * cmsTemplates.template.addAbeDataAttrForHtmlTag |
||
44 | * |
||
45 | */ |
||
46 | it('cmsTemplates.prepare.addAbeDataAttrForHtmlTag()', function() { |
||
47 | // stub |
||
48 | |||
49 | // test |
||
50 | var template = cmsTemplates.prepare.addAbeDataAttrForHtmlTag(this.fixture.text) |
||
51 | chai.expect(template.indexOf('<span data-abe-text_visible="text_visible" >')).to.be.above(-1); |
||
52 | |||
53 | template = cmsTemplates.prepare.addAbeDataAttrForHtmlTag(this.fixture.each) |
||
54 | chai.expect(template.indexOf('data-abe-test{{@index}}-title="test{{@index}}-title"')).to.be.above(-1) |
||
55 | |||
56 | // |
||
57 | try{ |
||
58 | template = cmsTemplates.prepare.addAbeHtmlTagBetweenAbeTags(this.fixture.noHtml) |
||
59 | template = cmsTemplates.prepare.addAbeDataAttrForHtmlTag(template) |
||
60 | } catch (e) { |
||
61 | console.log(e.stack) |
||
|
|||
62 | } |
||
63 | chai.expect(template.indexOf('"<abe data-abe-stores{{@index}}-lat="stores{{@index}}-lat"')).to.be.above(-1) |
||
64 | chai.expect(template.indexOf('<abe data-abe-stores2{{@index}}-lat="stores2{{@index}}-lat" >')).to.be.above(-1) |
||
65 | chai.expect(template.indexOf('<abe data-abe-text2="text2" >{{abe type="text" key="text2" desc="name"}}</abe>')).to.be.above(-1) |
||
66 | chai.expect(template.indexOf('"<abe data-abe-text3="text3" >{{abe type="text" key="text3" desc="name"}}</abe>"')).to.be.above(-1) |
||
67 | }); |
||
68 | |||
69 | /** |
||
70 | * cmsTemplates.template.getAbeAttributeData |
||
71 | * |
||
72 | */ |
||
73 | it('cmsTemplates.prepare.getAbeAttributeData()', function() { |
||
74 | // stub |
||
75 | |||
76 | // test |
||
77 | var template = cmsTemplates.prepare.getAbeAttributeData( |
||
78 | this.fixture.attribute, |
||
79 | "src=\"{{abe type='image' key='image_key' tab='default'}}\"", |
||
80 | "src", |
||
81 | "{{abe type='image' key='image_key' tab='default'}}" |
||
82 | ) |
||
83 | chai.expect(template.indexOf('data-abe-attr-image_key="src" data-abe-image_key="image_key"')).to.be.above(-1); |
||
84 | }); |
||
85 | |||
86 | /** |
||
87 | * cmsTemplates.template.addHasAbeAttr |
||
88 | * |
||
89 | */ |
||
90 | it('cmsTemplates.prepare.addHasAbeAttr()', function() { |
||
91 | // stub |
||
92 | |||
93 | // test |
||
94 | var template = cmsTemplates.prepare.addHasAbeAttr("}}") |
||
95 | chai.expect(template.indexOf('has-abe=1}}')).to.be.above(-1); |
||
96 | }); |
||
97 | |||
98 | /** |
||
99 | * cmsTemplates.template.addAbeDataAttrForHtmlAttributes |
||
100 | * |
||
101 | */ |
||
102 | it('cmsTemplates.prepare.addAbeDataAttrForHtmlAttributes()', function() { |
||
103 | // stub |
||
104 | |||
105 | // test |
||
106 | var template = cmsTemplates.prepare.addAbeDataAttrForHtmlAttributes(this.fixture.attribute) |
||
107 | chai.expect(template.indexOf('data-abe-attr-image_key="src" data-abe-image_key="image_key"')).to.be.above(-1); |
||
108 | |||
109 | template = cmsTemplates.prepare.addAbeDataAttrForHtmlAttributes(this.fixture.attributeConcat) |
||
110 | chai.expect(template.indexOf('data-abe-attr-image_key="src" data-abe-image_key="image_key"')).to.be.above(-1); |
||
111 | |||
112 | template = cmsTemplates.prepare.addAbeDataAttrForHtmlAttributes(this.fixture.attributeMultiple) |
||
113 | chai.expect(template.indexOf('data-abe-attr-image_key="src" data-abe-image_key="image_key"')).to.be.above(-1); |
||
114 | chai.expect(template.indexOf('data-abe-attr-alternate="alt" data-abe-alternate="alternate" alt="mon alt')).to.be.above(-1); |
||
115 | |||
116 | template = cmsTemplates.prepare.addAbeDataAttrForHtmlAttributes(this.fixture.each) |
||
117 | chai.expect(template.indexOf('data-abe-attr-test[index].img="src" data-abe-test[index].img="test[index].img" data-abe-attr-test{{@index}}.img="src" data-abe-test{{@index}}.img="test[index].img" src="')).to.be.above(-1); |
||
118 | |||
119 | template = cmsTemplates.prepare.addAbeDataAttrForHtmlAttributes(this.fixture.eachMultiple) |
||
120 | chai.expect(template.indexOf('data-abe-attr-test[index].img="src" data-abe-test[index].img="test[index].img" data-abe-attr-test{{@index}}.img="src" data-abe-test{{@index}}.img="test[index].img" src="')).to.be.above(-1); |
||
121 | chai.expect(template.indexOf('data-abe-attr-test[index].alternate="alt" data-abe-test[index].alternate="test[index].alternate" data-abe-attr-test{{@index}}.alternate="alt" data-abe-test{{@index}}.alternate="test[index].alternate" alt="')).to.be.above(-1); |
||
122 | }); |
||
123 | |||
124 | /** |
||
125 | * cmsTemplates.template.addAbeSourceComment |
||
126 | * |
||
127 | */ |
||
128 | it('cmsTemplates.prepare.addAbeSourceComment()', function() { |
||
129 | var template = cmsTemplates.prepare.addAbeSourceComment(this.fixture.source, |
||
130 | { |
||
131 | abe_source: { |
||
132 | data_key: [{title: "test"}] |
||
133 | } |
||
134 | } |
||
135 | ) |
||
136 | |||
137 | chai.expect(template.indexOf('<!-- [[')).to.be.above(-1); |
||
138 | chai.expect(template.indexOf('-->')).to.be.above(-1); |
||
139 | }); |
||
140 | |||
141 | it('cmsTemplates.prepare.addAbeSourceComment() each', function() { |
||
142 | var template = cmsTemplates.prepare.addAbeSourceComment(this.fixture.each, { |
||
143 | abe_source: { |
||
144 | test: [{title: "test"}] |
||
145 | } |
||
146 | }) |
||
147 | |||
148 | chai.expect(template.indexOf('<!-- [[')).to.be.above(-1); |
||
149 | chai.expect(template.indexOf('-->')).to.be.above(-1); |
||
150 | }); |
||
151 | |||
152 | it('cmsTemplates.prepare.addAbeSourceComment() eachMultiple', function() { |
||
153 | var template = cmsTemplates.prepare.addAbeSourceComment(this.fixture.eachMultiple, { |
||
154 | abe_source: { |
||
155 | test: [{title: "test"}] |
||
156 | } |
||
157 | }) |
||
158 | |||
159 | chai.expect(template.indexOf('<!-- [[')).to.be.above(-1); |
||
160 | chai.expect(template.indexOf('-->')).to.be.above(-1); |
||
161 | }); |
||
162 | |||
163 | it('cmsTemplates.prepare.addAbeSourceComment() eachVariable', function() { |
||
164 | var template = cmsTemplates.prepare.addAbeSourceComment(this.fixture.eachVariable, { |
||
165 | abe_source: { |
||
166 | test: [{title: "test"}] |
||
167 | } |
||
168 | }) |
||
169 | |||
170 | chai.expect(template.indexOf('<!-- [[')).to.be.above(-1); |
||
171 | chai.expect(template.indexOf('-->')).to.be.above(-1); |
||
172 | }); |
||
173 | |||
174 | /** |
||
175 | * cmsTemplates.template.addAbeHtmlTagBetweenAbeTags |
||
176 | * |
||
177 | */ |
||
178 | it('cmsTemplates.prepare.addAbeHtmlTagBetweenAbeTags()', function() { |
||
179 | // stub |
||
180 | |||
181 | // test |
||
182 | var template = cmsTemplates.prepare.addAbeHtmlTagBetweenAbeTags(this.fixture.text) |
||
183 | chai.expect(template.indexOf('<abe>{{')).to.be.above(-1); |
||
184 | chai.expect(template.indexOf('}}</abe>')).to.be.above(-1); |
||
185 | |||
186 | template = cmsTemplates.prepare.addAbeHtmlTagBetweenAbeTags(this.fixture.noHtml) |
||
187 | chai.expect(template.indexOf('"lat": "<abe>{{abe type="text" key="stores.lat" desc="lat"}}</abe>"')).to.be.above(-1) |
||
188 | chai.expect(template.indexOf('<abe>{{abe type="text" key="stores2.lat" desc="lat"}}</abe>')).to.be.above(-1) |
||
189 | chai.expect(template.indexOf('<abe>{{abe type="text" key="text2" desc="name"}}</abe>')).to.be.above(-1) |
||
190 | chai.expect(template.indexOf('"<abe>{{abe type="text" key="text3" desc="name"}}</abe>"')).to.be.above(-1) |
||
191 | |||
192 | }); |
||
193 | |||
194 | /** |
||
195 | * cmsTemplates.template.replaceAbeEachIndex |
||
196 | * |
||
197 | */ |
||
198 | it('cmsTemplates.prepare.replaceAbeEachIndex()', function() { |
||
199 | var template = cmsTemplates.prepare.replaceAbeEachIndex('[index].') |
||
200 | chai.expect(template).to.be.equal('{{@index}}-'); |
||
201 | }); |
||
202 | |||
203 | /** |
||
204 | * cmsTemplates.template.removeHiddenAbeTag |
||
205 | * |
||
206 | */ |
||
207 | it('cmsTemplates.prepare.removeHiddenAbeTag()', function() { |
||
208 | var template = cmsTemplates.prepare.removeHiddenAbeTag(this.fixture.visibleFalse) |
||
209 | chai.expect(template).to.be.equal(""); |
||
210 | |||
211 | var template2 = cmsTemplates.prepare.removeHiddenAbeTag(this.fixture.visibleTrue) |
||
212 | chai.expect(template2.indexOf('{{abe')).to.be.above(-1); |
||
213 | }); |
||
214 | |||
215 | /** |
||
216 | * cmsTemplates.template.removeHandlebarsRawFromHtml |
||
217 | * |
||
218 | */ |
||
219 | it('cmsTemplates.prepare.removeHandlebarsRawFromHtml()', function() { |
||
220 | var template = cmsTemplates.prepare.removeHandlebarsRawFromHtml(this.fixture.rawHandlebar) |
||
221 | chai.expect(template).to.be.equal("test"); |
||
222 | }); |
||
223 | |||
224 | /** |
||
225 | * cmsTemplates.template.splitEachBlocks |
||
226 | * |
||
227 | */ |
||
228 | it('cmsTemplates.prepare.splitEachBlocks()', function() { |
||
229 | var blocks = cmsTemplates.prepare.splitEachBlocks(this.fixture.each) |
||
230 | chai.expect(blocks.length).to.be.above(0); |
||
231 | }); |
||
232 | |||
233 | /** |
||
234 | * cmsTemplates.template.indexEachBlocks |
||
235 | * |
||
236 | */ |
||
237 | it('cmsTemplates.prepare.indexEachBlocks() each', function() { |
||
238 | var template = cmsTemplates.prepare.indexEachBlocks(this.fixture.each, {}, false) |
||
239 | chai.expect(template.indexOf('abe dictionnary=')).to.be.above(-1) |
||
240 | }); |
||
241 | |||
242 | // eachMultiplePrepared is the string obtained after all other functions have "prepared" the string |
||
243 | // and just before indexEachBlocks() is triggered |
||
244 | it('cmsTemplates.prepare.indexEachBlocks() eachMultiplePrepared', function() { |
||
245 | var template = cmsTemplates.prepare.indexEachBlocks(this.fixture.eachMultiplePrepared, {}, false) |
||
246 | console.log(template) |
||
247 | var count = (template.match(/abe dictionnary=/g) || []).length; |
||
248 | chai.expect(count).to.be.equal(2) |
||
249 | }); |
||
250 | |||
251 | it('cmsTemplates.prepare.indexEachBlocks() eachMultiple with data', function() { |
||
252 | var template = cmsTemplates.prepare.indexEachBlocks( |
||
253 | this.fixture.eachMultiple, |
||
254 | { |
||
255 | abe_source: { |
||
256 | test: [{title: "test"}] |
||
257 | } |
||
258 | }, |
||
259 | false |
||
260 | ) |
||
261 | chai.expect(template.indexOf('abe dictionnary=')).to.be.above(-1) |
||
262 | chai.expect(template.indexOf('<!-- [[')).to.be.equal(-1); |
||
263 | }); |
||
264 | |||
265 | it('cmsTemplates.prepare.indexEachBlocks() eachVariable', function() { |
||
266 | var template = cmsTemplates.prepare.indexEachBlocks(this.fixture.eachVariable, {}, false) |
||
267 | chai.expect(template.indexOf('abe dictionnary=')).to.be.above(-1) |
||
268 | chai.expect(template.indexOf('{{formatted_address}} - (lat:{{geometry.location.lat}}-lng:{{geometry.location.lng}})')).to.be.above(-1) |
||
269 | }); |
||
270 | |||
271 | /** |
||
272 | * cmsTemplates.template.addAbeDictionnary |
||
273 | * |
||
274 | */ |
||
275 | it('cmsTemplates.prepare.addAbeDictionnary()', function() { |
||
276 | // stub |
||
277 | |||
278 | // test |
||
279 | var template = cmsTemplates.prepare.addAbeDictionnary(this.fixture.each, "{{abe type='text' key='test.title' desc='test title' tab='default'}}", 'test') |
||
280 | chai.expect(template.indexOf("abe dictionnary='test'")).to.be.above(-1); |
||
281 | }); |
||
282 | }); |
||
283 |